<?php
/**
 * Created by cmb_weixin.master.
 * File: Customer.php
 * User: xuewl
 * Date: 2017/12/20
 * Time: 00:04
 */

namespace app\api\controller\v1\user;
use app\api\controller\v1\Init;

class Customer extends Init
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 客户经理列表
     */
    public function lists() {
        $maps = [
            'status' => 1,
            'role'  => 2
        ];
        $result = \think\Db::name('user')
            ->field("id,customer_realname,customer_pinyin")
            ->where($maps)
            ->select();
        $result = \fast\ArrayHelper::map($result, 'id', '', 'customer_pinyin', true);
        return $this->response('success', $result);


    }


    /**
     * 贷款订单列表
     */
    public function product_order_lists() {
        $maps = ['status' => 0];
        if(isset($this->params['stage'])) {
            $maps['stage'] = (int) $this->params['stage'];
        }
        if(isset($this->params['customer_id'])) {
            $maps['customer_id'] = (int) $this->params['customer_id'];
        }

        $extra = ['product', 'user'];
        $result = model('product/product_order', 'service')->lists($maps, $this->limit, $this->page, $this->params['order'], true, $extra);

        foreach ($result AS $k => $v) {
            $result[$k]->is_reserve = \think\Db::name('product_order_reserve')->where([
                'order_sn' => $v['sn'],
                'customer_id' => $this->user['id']
            ])->count();
        }


        return $this->response('success', $result);
    }

    /**
     * 客户经理抢单
     * @return \think\Response
     */
    public function product_order_reserve() {
        $model = model('product/product_order_reserve', 'service');
        $result = $model->create($this->params['order_sn'], $this->user['id']);
        if(!$result) {
            return $this->response($model->getError(), [], -20002);
        }
        return $this->response('success', $result);
    }

    /**
     * 贷款抢单审批
     * @return \think\Response
     */
    public function product_order_reserve_review() {
        $model = model('product/product_order_reserve', 'service');
        $result = $model->review($this->params['order_sn'], $this->user['id']);
        if(!$result) {
            return $this->response($model->getError(), [], -20002);
        }
        return $this->response('success', $result);
    }


    public function product_order_transfer_view() {
        $model = model('product/product_order_transfer', 'service');
        $result = $model->where([
            'order_sn' => $this->params['order_sn'],
            'touid' => $this->user['id']
        ])->find();
        if($this->params['attrs']) {
            $attrs = explode(",",$this->params['attrs']);
            array_map(function($attr) use (&$result) {
                $attr = trim($attr);
                return $result->$attr = $result->getAttr($attr);
            }, $attrs);
        }
        if($this->params['extra']) {
            $extra = (is_string($this->params['extra'])) ? explode(",", $this->params['extra']) : $this->params['extra'];
            array_map(function($e) use (&$result) {
                $e = trim($e);
                return $result->$e;
            }, $extra);
        }

        if(!$result) {
            return $this->response($model->getError(), [], -20002);
        }
        return $this->response('success', $result);
    }


    /**
     * 客户经理订单转交申请
     * @return \think\Response
     */
    public function product_order_transfer_create() {
        $model = model('product/product_order_transfer', 'service');
        $result = $model->create($this->params['order_sn'], $this->params['touid'], $this->user['id']);
        if(!$result) {
            return $this->response($model->getError(), [], -20002);
        }
        return $this->response('success', $result);
    }

    /**
     * 客户经理订单转交接收
     * @return \think\Response
     */
    public function product_order_transfer_receive() {
        $model = model('product/product_order_transfer', 'service');
        $result = $model->receive($this->params['order_sn'], $this->params['status'], $this->user['id']);
        if(!$result) {
            return $this->response($model->getError(), [], -20002);
        }
        return $this->response('success', $result);
    }

}